package com.czy.owner.ui.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.GrowthValueCouponAdapter;
import com.czy.owner.api.GrowthValueDetailsApi;
import com.czy.owner.api.ReceivedCouponsApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.GrowthDetailsModel;
import com.czy.owner.entity.GrowthValueCouponList;
import com.czy.owner.entity.WebViewRequestParam;
import com.czy.owner.global.Constants;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.WebViewActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.StatusBarUtil;
import com.czy.owner.widget.MyRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.czy.owner.entity.WebViewRequestParam.REQUEST_POST;

public class GrowthValueActivity extends BaseActivity {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.img_top_head)
    ImageView imgTopHead;
    @BindView(R.id.tv_user_group)
    TextView tvUserGroup;
    @BindView(R.id.tv_disparity)
    TextView tvDisparity;

    @BindView(R.id.linear_user_group)
    LinearLayout linearUserGroup;
    @BindView(R.id.tv_tab_left)
    TextView tvTabLeft;
    @BindView(R.id.tv_tab_right)
    TextView tvTabRight;
    @BindView(R.id.card_view)
    CardView cardView;
    @BindView(R.id.seek_bar_growth)
    SeekBar seekBarGrowth;
    GrowthValueCouponAdapter growthValueCouponAdapter;
    @BindView(R.id.recycler_view_list)
    MyRecyclerView recyclerViewList;
    @BindView(R.id.linear_details)
    LinearLayout linearDetails;
    @BindView(R.id.linear_tab_left)
    LinearLayout linearTabLeft;
    @BindView(R.id.linear_tab_right)
    LinearLayout linearTabRight;
    @BindView(R.id.tv_current_member)
    TextView tvCurrentMember;
    @BindView(R.id.img_current_member)
    ImageView imgCurrentMember;
    @BindView(R.id.tv_next_member)
    TextView tvNextMember;
    @BindView(R.id.img_next_member)
    ImageView imgNextMember;
    @BindView(R.id.tv_next_member_num_1)
    TextView tvNextMemberNum1;
    @BindView(R.id.tv_next_member_num_2)
    TextView tvNextMemberNum2;
    @BindView(R.id.tv_coupon_empty)
    TextView tvCouponEmpty;

    @BindView(R.id.relat_seek)
    RelativeLayout relatSeek;
    @BindView(R.id.linear_coupon_empty)
    LinearLayout linearCouponEmpty;
    @BindView(R.id.view_line)
    TextView viewLine;
    @BindView(R.id.linear_current_member)
    LinearLayout linearCurrentMember;
    @BindView(R.id.linear_next_member)
    LinearLayout linearNextMember;
    @BindView(R.id.view_left)
    View viewLeft;
    @BindView(R.id.view_right)
    View viewRight;
    private String session;
    public int[] mTabRes = new int[]{R.mipmap.img_bronze_member, R.mipmap.img_silver_member, R.mipmap.img_gold_member,
            R.mipmap.img_platinum_member, R.mipmap.img_diamonds_member};
    public int[] mTabResNextUserGrowth = new int[]{R.mipmap.img_bronze_gray, R.mipmap.img_silver_gray, R.mipmap.img_gold_gray,
            R.mipmap.img_platinum_gray, R.mipmap.img_diamonds_gray};
    public int[] mTabResUserGrowth = new int[]{R.mipmap.img_bronze, R.mipmap.img_silver, R.mipmap.img_gold,
            R.mipmap.img_platinum, R.mipmap.img_diamonds};
    //    public String[] mTabTilte = new String[]{"青铜会员", "亮银会员", "黄金会员",
//            "铂金会员", "钻石会员"};
    private int currentPosition;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_growth_value;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        //
        seekBarGrowth.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO 返回true 事件被消费，点击无效果
                return true;
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        MyLog.e("hep", imgCurrentMember.getHeight() + "--" + imgCurrentMember.getWidth());
        MyLog.e("hep", linearCurrentMember.getHeight() + "--" + linearCurrentMember.getWidth());

        int leftWidth = linearCurrentMember.getWidth() - (linearCurrentMember.getWidth() - imgCurrentMember.getWidth()) / 2;
        int rightWidth = linearNextMember.getWidth() - (linearNextMember.getWidth() - imgNextMember.getWidth()) / 2;
        MyLog.e("hep", leftWidth + "===");
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) seekBarGrowth.getLayoutParams();
        lp.setMargins(leftWidth - PhoneUtils.dip2px(this, 15), 0, rightWidth - PhoneUtils.dip2px(this, 15), 0);
        seekBarGrowth.setLayoutParams(lp);

        RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) viewLeft.getLayoutParams();
        lp1.setMargins(PhoneUtils.dip2px(this, 20), 0, leftWidth, 0);
        viewLeft.setLayoutParams(lp1);

        RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams) viewRight.getLayoutParams();
        lp2.setMargins(rightWidth, 0, PhoneUtils.dip2px(this, 20), 0);
        viewRight.setLayoutParams(lp2);

    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white));
        StatusBarUtil.StatusBarLightMode(this);

    }

    @Override
    protected void updateViews() {
        toolbarTitle.setText("袋鼠成长值");
        toolbar.setNavigationIcon(R.mipmap.icon_back_black);
        toolbar.setBackgroundColor(this.getResources().getColor(R.color.white));
        toolbarTitle.setTextColor(this.getResources().getColor(R.color.appThemeSubTitleColor));
        session = UserHelper.getInstance().getSessionInfoModel(this).getSession();
        seekBarGrowth.setPadding(PhoneUtils.dip2px(this, 15), 0, PhoneUtils.dip2px(this, 15), 0);
        growthValueCouponAdapter = new GrowthValueCouponAdapter(this);
        recyclerViewList.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewList.setAdapter(growthValueCouponAdapter);
        growthValueCouponAdapter.setOnGetCouponsSuccess(new GrowthValueCouponAdapter.OnGetCouponsSuccess() {
            @Override
            public void onSuccess(GrowthValueCouponList data) {
                getCoupons(data);
            }
        });
        getData();
    }

    /*
    * 成长值详情
    * **/
    public void getData() {
        GrowthValueDetailsApi growthValueDetailsApi = new GrowthValueDetailsApi(new HttpOnNextListener<GrowthDetailsModel>() {
            @Override
            public void onNext(GrowthDetailsModel model) {
                currentPosition = Integer.parseInt(model.getUserGrowthValue().getSettingName().substring(model.getUserGrowthValue().getSettingName().length() - 1));
//                currentPosition = 1;
                tvUserGroup.setText(model.getUserGrowthValue().getGrowthValue());
                imgTopHead.setImageResource(mTabRes[currentPosition - 1]);
//                currentPosition=5;
                if (currentPosition == 5) {
                    relatSeek.setVisibility(View.GONE);
                    tvDisparity.setVisibility(View.GONE);
                    viewLine.setVisibility(View.GONE);
                } else {
                    if (model.getNextLevel() == null) {
                        model.setNextLevel(new GrowthDetailsModel.NextLevelModel());
                    }
                    tvDisparity.setText("离升级" + model.getNextLevel().getDescription() + "还差" + model.getDisValue() + "点成长值");
                    tvCurrentMember.setText(model.getUserGrowthValue().getDescription());

                    tvNextMember.setText(model.getNextLevel().getDescription());

                    imgCurrentMember.setImageResource(mTabResUserGrowth[currentPosition - 1]);
                    imgNextMember.setImageResource(mTabResNextUserGrowth[currentPosition]);
                    tvNextMemberNum1.setText(model.getUserGrowthValue().getGrowthValue());
                    tvNextMemberNum2.setText("/" + model.getNextLevel().getSettingValue());
                    seekBarGrowth.setMax(Integer.parseInt(model.getNextLevel().getSettingValue()));
                    seekBarGrowth.setProgress(Integer.parseInt(model.getUserGrowthValue().getGrowthValue()));
                }
                if (model.getCouponsList() == null || model.getCouponsList().size() == 0) {
                    linearCouponEmpty.setVisibility(View.VISIBLE);
                    recyclerViewList.setVisibility(View.GONE);
                } else {
                    linearCouponEmpty.setVisibility(View.GONE);
                    recyclerViewList.setVisibility(View.VISIBLE);
                    growthValueCouponAdapter.clear();
                    growthValueCouponAdapter.addAll(model.getCouponsList());
                }
            }

            @Override
            public void onError(Throwable e) {
                MyLog.e("hep", e.toString());
                super.onError(e);
            }
        }, this);
        growthValueDetailsApi.setSession(session);

        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(growthValueDetailsApi);
    }

    /*
    * 领取优惠券
    * **/
    public void getCoupons(final GrowthValueCouponList data) {
        ReceivedCouponsApi receivedCouponsApi = new ReceivedCouponsApi(new HttpOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
                getData();
                PhoneUtils.ShowToastMessage(GrowthValueActivity.this, "领取优惠券成功");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        }, GrowthValueActivity.this);
        receivedCouponsApi.setSession(UserHelper.getInstance().getSessionInfoModel(GrowthValueActivity.this).getSession());
        receivedCouponsApi.setCouponsGroupId(data.getCouponsGroupId());
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(receivedCouponsApi);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.linear_tab_left, R.id.linear_tab_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            // 规则
            case R.id.linear_tab_left:
                String url = Constants.APP_API_BASE + Constants.OWN_OWNERGROWTHVALUERULE;
                WebViewRequestParam webViewRequestParam = new WebViewRequestParam(url);
                webViewRequestParam.setRequestMethod(REQUEST_POST);

                Intent intent = new Intent(GrowthValueActivity.this, WebViewActivity.class);
                intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                intent.putExtra("type", "growth");
                startActivityForResult(intent, 848);
                break;
            // 获取记录
            case R.id.linear_tab_right:
                startActivity(new Intent(GrowthValueActivity.this, GrowthValueListActivity.class));
                break;
        }
    }
}
